package pswvclass.pswvandroid2.com.more;

import android.content.ContentValues;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;

import com.alipay.sdk.app.EnvUtils;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.HashMap;
import java.util.Map;

import pswvclass.pswvandroid2.R;
import pswvclass.pswvandroid2.com.Home;
import pswvclass.pswvandroid2.com.Login;
import pswvclass.pswvandroid2.com.more.Wallet.Wallet;
import pswvclass.pswvandroid2.com.user.SafeSetInner;
import pswvclass.pswvandroid2.com.user.UserDatumSee;
import pswvclass.pswvandroid2.com.util.DBHelper;
import pswvclass.pswvandroid2.com.util.HttpUtils;

/**
 */
public class MoreMenu extends Fragment implements View.OnClickListener {
    private String tag = "MoreMenu";
    private View my_wallet;
    private View quit;
    private View safe_set;
    private View edit_material;
    private TextView user_name;
    private DBHelper db;
    private SQLiteDatabase sd;
    protected static final int ERROR = 1;
    protected static final int SEE_SUCCESS = 0;
    private Handler mHandler = new Handler() {
        public void handleMessage(Message msg) {
            switch (msg.what) {
               /* case SEE_SUCCESS:
                    JSONObject jsonObj= (JSONObject) msg.obj;
                   try {
                      user_name.setText(jsonObj.getString("name"));
                    } catch (JSONException e) {
                        e.printStackTrace();
                    }
                    break;*/
                case ERROR:
                    break;
                default:
                    break;
            }
        };
    };
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {

        return inflater.inflate(R.layout.pswv_more_menu, null);
    }

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        EnvUtils.setEnv(EnvUtils.EnvEnum.SANDBOX);
        user_name= (TextView) getView().findViewById(R.id.user_name);
        my_wallet = getActivity().findViewById(R.id.my_wallet);
        my_wallet.setOnClickListener(this);
        quit = getActivity().findViewById(R.id.quit);
        quit.setOnClickListener(this);
        safe_set = getActivity().findViewById(R.id.safe_set);
        safe_set.setOnClickListener(this);
        edit_material = getActivity().findViewById(R.id.edit_material);
        edit_material.setOnClickListener(this);
        DBHelper db = new DBHelper(getContext());
        SQLiteDatabase sd = db.getWritableDatabase();
        int userId = 0;
        Cursor cursor = sd.query("user", new String[]{"userId"}, "state=?", new String[]{"1"}, null, null, null);
        while (cursor.moveToNext()) {
            userId = cursor.getInt(0);
        }
        cursor.close();
        final int finalUserId = userId;
       /* new Thread() {
            public void run() {
                try {
                    HttpUtils hu = new HttpUtils();
                    String data2;
                    final String src = getResources().getString(R.string.src_mysql) + "pswv/api/userAction/queryUserById";
                    final Map<String, String> map1 = new HashMap<String, String>();
                    map1.put("userId", finalUserId + "");
                    data2 = hu.submitPostData(src, map1, "utf-8");
                    JSONObject jsonObj = new JSONObject(data2);
                    JSONObject objectt = jsonObj.getJSONObject("response");
                    int result = Integer.parseInt(jsonObj.getString("code"));
                    if (result == 0) {
                        Message msg = Message.obtain();
                        msg.obj=objectt;
                        msg.what = SEE_SUCCESS;
                        mHandler.sendMessage(msg);
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                    Message msg = Message.obtain();
                    msg.what = ERROR;
                    mHandler.sendMessage(msg);
                    Log.v(tag, e + "");
                }
            }
        }.start();*/
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.my_wallet: {
                Intent intent = new Intent();
                intent.setClass(getContext(), Wallet.class);
                startActivity(intent);
                break;
            }
            case R.id.edit_material: {
                getFragmentManager()
                        .beginTransaction()
                        .addToBackStack(null)
                        .replace(R.id.realtabcontent, new UserDatumSee()).commit();
                break;
            }
            case R.id.quit: {
                db = new DBHelper(getContext());
                sd = db.getWritableDatabase();
                ContentValues values = new ContentValues();
                values.put(DBHelper.STATE, 0);
                sd.update("user", values, "state=1", null);
                Intent intent = new Intent();
                intent.setClass(getContext(), Login.class);
                startActivity(intent);
                break;
            }
            case R.id.safe_set: {
                Intent intent = new Intent();
                intent.setClass(getContext(), SafeSetInner.class);
                startActivity(intent);
                break;
            }
            default:
                break;
        }
    }
    @Override
    public void onResume() {
        super.onResume();
        getView().setFocusableInTouchMode(true);
        getView().requestFocus();
        getView().setOnKeyListener(new View.OnKeyListener() {
            @Override
            public boolean onKey(View view, int i, KeyEvent keyEvent) {
                if (keyEvent.getAction() == KeyEvent.ACTION_DOWN && i == KeyEvent.KEYCODE_BACK) {
                    Button my_page = (Button) getActivity().findViewById(R.id.my_page);
                    Button kword_page = (Button) getActivity().findViewById(R.id.kword_page);
                    Button risk_page = (Button) getActivity().findViewById(R.id.risk_page);
                    Button need_page = (Button) getActivity().findViewById(R.id.need_page);
                    my_page.setVisibility(getView().VISIBLE);
                    kword_page.setVisibility(getView().VISIBLE);
                    risk_page.setVisibility(getView().VISIBLE);
                    need_page.setVisibility(getView().VISIBLE);
                    my_page.setBackground(getView().getContext().getResources().getDrawable(R.drawable.ditu1));
                    kword_page.setBackground(getView().getContext().getResources().getDrawable(R.drawable.ditu1));
                    risk_page.setBackground(getView().getContext().getResources().getDrawable(R.drawable.ditu1));
                    need_page.setBackground(getView().getContext().getResources().getDrawable(R.drawable.ditu1));
                    getFragmentManager()
                            .beginTransaction()
                            .addToBackStack(null)
                            .replace(R.id.realtabcontent, new Home()).commit();
                    return true;
                }
                return false;
            }
        });
    }
}
